// 二次封装axios
import axios from 'axios'
import qs from "qs";
import { sign } from "@/components/Dialog/DragVerify";

const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,
  withCredentials: false,
  timeout: 20000
  // httpAgent: new window["http"].Agent({ keepAlive: true }),
});
service.interceptors.request.use(config=> {
  const data = config.data || config.params;
  let token = localStorage.getItem("token");
  if (token) {
    config.headers["cqliving_server_token"] = token;
  }
  const url = config.url;
  let flag = true;
  // 人机校验
  if (url?.includes("/captcha/get") || url?.includes("/captcha/check")) {
    sign(config);
  }
  if (flag) {
    config.data = qs.stringify(data);
  }

  return config;

}, error=> {
  return Promise.reject(error);
})


service.interceptors.response.use(
  resp => {
    // TODO http异常代码处理
    // TODO 接口自定义请求异常处理
    return resp.data;
  },
  error => {
    return Promise.reject(error);
  }
);

export default service;